import {
  GET_USERINFO,
  SET_TOKEN,
  SET_USERINFO
} from './mutation-types.js'

export default {
  [GET_USERINFO](state) {
    if (state.userInfo) {
      return state.userInfo
    }
    return JSON.parse(localStorage.getItem('dangjianUserInfo'))
  },
  [SET_TOKEN](state, token) {
    if (token) {
      state.token = token
      localStorage.setItem('dangjianToken', token)
    } else {
      state.token = ''
      localStorage.removeItem('dangjianToken')
    }
  },
  [SET_USERINFO](state, userInfo) {
    if (userInfo) {
      state.userInfo = userInfo
      localStorage.setItem('dangjianUserInfo', JSON.stringify(userInfo))
    } else {
      state.userInfo = null
      localStorage.removeItem('dangjianUserInfo')
    }
  }
}
